Function.prototype.myCall = function (thisArg, ...args) {
  thisArg = thisArg || globalThis
  const fn = Symbol()
  thisArg[fn] = this
  const result = thisArg[fn](...args)
  Reflect.deleteProperty(thisArg, fn)
  return result
}

function add(a, b) {
  console.log(this)
  return a + b
}

const a = {a: 1}
// console.log(add.call(a, 1, 2))
// console.log(add.myCall(a,1,2))

// Function.prototype.myApply = function (thisArgs, arrArgs) {
//   thisArgs ||= globalThis
//   const fn = Symbol()
//   thisArgs[fn] = this
//
//   const result = thisArgs[fn](...arrArgs)
//   Reflect.deleteProperty(thisArgs, fn)
//
//   return result
//
// }
//
// console.log(add.apply(a, [1, 2]))
// console.log(add.myApply(a, [1, 2]))


Function.prototype.myBind = function (thisArgs, ...argsArray){
  thisArgs ||= globalThis
  const fn = Symbol()
  thisArgs[fn] = this
  return function (...args) {
    const result = thisArgs[fn](...argsArray,...args)
    Reflect.deleteProperty(thisArgs,fn)
    return result

  }
}

const fn = add.bind(a,1)
console.log(fn(2))

// const fn = add.myBind(a,1)
// console.log(fn(2))